﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace 合并页面
{
    public class MyArrayList : ArrayList, System.Collections.IComparer
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public override bool Contains(object item)
        {
            if (this.Count == 0)
                return false;
            else
            {
                string clsName = item.GetType().FullName; //得到传入对象的类名
                Type type = Type.GetType(clsName);        //根据类名实例化。
                System.Reflection.PropertyInfo ps=null;
                string fileName = "";
                if (type.Name != "String")
                {
                    ps = type.GetProperty("FileName");
                    fileName = (string)ps.GetValue(item, null);
                }
                else
                {
                    fileName = (string)item + ".jpg";
                }
                //string fileName = (string)ps.GetValue(item,null);
                for (int i = 0; i < this.Count; i++)
                {
                    string temp = "";
                    if (type.Name != "String")
                    {
                        temp = (string)ps.GetValue(this[i], null);
                    }
                    else
                    {
                        temp = ((ImageFile)this[i]).FileName;
                    }
                    if (temp == fileName)
                        return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 根据文件名得到全路径名。
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public string FindeByFileName(string title)
        {
            for (int i = 0; i < this.Count; i++)
            {
                if (((ImageFile)this[i]).FileName == title+".jpg")
                    return ((ImageFile)this[i]).FullPath;
            }
            return "";
        }

        #region IComparer 成员
        public int Compare(object x, object y)
        {
            string clsName = x.GetType().FullName; //得到传入对象的类名
            Type type = Type.GetType(clsName);        //根据类名实例化。
            System.Reflection.PropertyInfo ps = type.GetProperty("FileName");
            string tempX = (string)ps.GetValue(x, null);
            string tempY = (string)ps.GetValue(y, null);
            return String.Compare(tempX,tempY);
        }
        #endregion

        /// <summary>
        /// ArrayList向后移动一个元素.
        /// </summary>
        /// <param name="list">需要移动的ArrayList</param>
        /// <param name="pos">移动元素的位置</param>
        /// <returns>返回移动后的List</returns>
        public MyArrayList NextList(int pos)
        {
            if (pos != this.Count)
            {
                object obj = this[pos + 1];
                this[pos + 1] = this[pos];
                this[pos] = obj;
            }
            return this;
        }

        public MyArrayList ToHeadList(int pos)
        {
            object obj = this[pos];
            for (int i = pos; i >= 1; i--)
            {
                this[i] = this[i - 1];
            }
            this[0] = obj;
            return this;
        }
        public MyArrayList ToTailList(int pos)
        {
            object obj = this[pos];
            for (int i = pos; i < this.Count-1; i++)
            {
                this[i] = this[i + 1];
            }
            this[this.Count-1] = obj;
            return this;
        }

        /// <summary>
        /// ArrayList向前移动一个元素.
        /// </summary>
        /// <param name="list">需要移动的ArrayList</param>
        /// <param name="pos">移动元素的位置</param>
        /// <returns>返回移动后的List</returns>
        public MyArrayList PreList(int pos)
        {
            if (pos != 0)
            {
                object obj = this[pos - 1];
                this[pos - 1] = this[pos];
                this[pos] = obj;
            }
            return this;
        }
    }
}